package com.telit.common.config;


import com.telit.common.service.MinioService;
import com.telit.common.service.ParamsCheckService;
import com.telit.common.util.MinioUtils;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
 * @Auther amy
 * @Describe minio配置
 * @ConditionalOnProperty 控制该configuration是否生效,havingValue为true就是加载
 */
@Configuration
public class MinioConfiguration {

    @Autowired
    private MinioProperties minioProperties;

    @Bean
    @Conditional(value = {MinioCondition.class})
    public MinioClient minioClient(){
        MinioClient client = MinioClient.builder()
                .endpoint(minioProperties.getUrl())
                .credentials(minioProperties.getAccessKey(),minioProperties.getSecretKey())
                .build();
        return client;
    }

    @Bean
    @ConditionalOnBean(MinioClient.class)
    public MinioService minioService(MinioClient minioClient, ParamsCheckService paramsCheckService){
        MinioService minioService = new MinioService();
        minioService.setMinioClient(minioClient);
        minioService.setExpireTime(minioProperties.getExpireTime());
        minioService.setPartSize(minioProperties.getPartSize());
        MinioUtils.setMinioUtils(minioService,paramsCheckService);
        return minioService;
    }

}
